package com.myself.source.module.filter;

import com.myself.common.define.model.CurrentUser;


public abstract class UserUtils {

    //线程变量，存放user实体类信息，即使是静态的与其他线程也是隔离的
    private static ThreadLocal<CurrentUser> userThreadLocal = new ThreadLocal<CurrentUser>();


    public static CurrentUser getLoginUser() {
        CurrentUser currentUser = userThreadLocal.get();
        return currentUser;
    }

    /**
     * 获取当前登录用户的ID
     * 未登录返回null
     *
     * @return
     */
    public static String getLoginUserId() {
        CurrentUser user = userThreadLocal.get();
        if (user != null && user.getUserId() != null) {
            return user.getUserId();
        }
        return null;
    }

    //为当前的线程变量赋值上用户信息
    public static void setLoginUser(CurrentUser user) {
        userThreadLocal.set(user);
    }

    //清除线程变量
    public static void removeUser() {
        userThreadLocal.remove();
    }
}
